<template>
  <div class="article-magin">
    <el-card class="box-card margin-30" shadow="hover">
      <div slot="header" class="clearfix">
        <span>条件查询</span>
      </div>
      <el-form ref="form" :model="searchArticleForm" label-width="80px">
        <el-row gutter="20">
          <el-col :span="6">
            <el-form-item label="作者">
              <!-- 根据searchArticleForm.title在接口中查找 -->
              <el-input v-model="searchArticleForm.author" placeholder="作者" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="标题">
              <!-- 根据searchArticleForm.author在接口中查找，title和author都能找到 -->
              <el-input v-model="searchArticleForm.title" placeholder="标题" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="编辑类型">
              <!-- editorType(integer,option1):编辑器类型：0 富文本；1 markdowm -->
              <el-select v-model="searchArticleForm.editorType" style="width:100%" placeholder="编辑类型" clearable>
                <el-option
                  label="富文本"
                  :value="0"
                />
                <el-option
                  label="markdown"
                  :value="1"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" :offset="18">
            <el-button type="default" size="mini" @click="resetForm">重置</el-button>
            <el-button type="primary" size="mini" @click="searchArticle">搜索</el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <!-- 文章列表展示 -->
    <el-card shadow="hover" class="margin-30">
      <div slot="header">
        <el-button type="primary" size="mini" @click="goAddArticle">新增</el-button>
      </div>
      <el-table v-loading="loading" :data="articleList" border stripe>
        <el-table-column
          label="#"
          type="index"
          width="50"
        />
        <el-table-column
          label="文章标题"
          prop="title"
          width="150"
        />
        <el-table-column
          label="展示图片"
          width="200"
        >
          <template slot-scope="scope">
            <img width="50" height="50" :src="scope.row.coverImg" alt="">
          </template>
        </el-table-column>
        <el-table-column
          label="添加时间"
          prop="createTime"
          width="200"
        />
        <el-table-column
          label="作者"
          prop="author"
          width="150"
        />
        <el-table-column
          label="文章是否展示"
          prop="author"
          width="200"
        >
          <!-- 添加是否显示开关 -->
          <template slot-scope="scope">
            <!-- isShow(integer,optional) 是否显示： 0 否; 1 是,-->
            <el-switch v-model="scope.row.isShow" :active-value="1" :inactive-value="0" @change="changeArticleShowStatus(scope.row.isShow,scope.row.id)" />
          </template>
        </el-table-column>
        <el-table-column
          label="编辑类型"
          prop="editorType"
          width="150"
          align="center"
        >
          <template slot-scope="scope">
            <!-- editorType(integer, optional)： 编辑器类型：0 富文本;1 markdown, -->
            <el-button v-if="scope.row.editorType==0" plain type="primary" size="small">富文本</el-button>
            <el-button v-if="scope.row.editorType==1" plain type="success" size="small">markdown</el-button>
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="200"
          align="center"
        >
          <template slot-scope="scope">
            <!-- editorType(integer, optional)： 编辑器类型：0 富文本;1 markdown, -->
            <el-button type="text" size="small" icon="el-icon-view">编辑文章</el-button>
            <el-button type="text" size="small" @click="goDeleteArticle(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 添加分页 -->
      <el-pagination
        style="margin-top:10px;text-align:right;"
        :current-page.sync="pagniationParams.start"
        :page-size="pagniationParams.limit"
        :page-sizes="pagniationParams.pageSizes"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pagniationParams.totalNum"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
  </div>
</template>
<script>
import mix from '@/mixins/index'
/* 为防止各种接口命名弄混，另起名字叫findArticlesApi */
import {
  findArticles as findArticlesApi,
  changeShowStatus as changeShowStatusApi,
  delArticle as delArticleApi
}
from '@/api/content/article'
import { filterNullParams } from '@/utils'
export default {
  name: 'Article',
  mixins: [mix],
  data() {
    return {
      loading: false, // 是否加载状态，默认为否
      searchArticleForm: {},
      articleList: []
    }
  },
  created() {
    this.getArticleList()
  },
  methods: {
    // 新增文章
    goAddArticle() {
      this.$router.push({ name: 'ArticleDetail' })
    },
    // 重置表单
    resetForm() {
      this.start = 1
      this.searchArticleForm = {}
      this.getArticleList()
    },
    // 搜索文章
    searchArticle() {
      this.start = 1
      this.getArticleList()
    },
    // 修改文章显示状态
    changeArticleShowStatus(isShow, id) {
      changeShowStatusApi({
        isShow,
        id
      }).then(res => {
        const { success, message } = res
        if (success) {
          this.$message.success('文章显示状态修改成功！')
        } else {
          this.$message.error(message)
        }
      })
    },
    // 删除文章
    goDeleteArticle(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 删除文章
        delArticleApi(id).then(res => {
          const { success, message } = res
          console.log(res)
          if (success) {
            // 刷新数据
            this.getArticleList()
          } else {
            this.$message.error(message)
          }
        })
      }).catch(() => {
        this.$message.info('你点击了取消！')
      })
    },
    // 获取文章列表
    getArticleList() {
      // 获取数据中，显示加载
      this.loading = true
      var newObj = filterNullParams(this.searchArticleForm)
      // 取第一页数据，每页10条
      findArticlesApi(this.pagniationParams.start, this.pagniationParams.limit, newObj)
        .then(res => {
        // console.log(res)
          const { success, data, message } = res
          if (success) {
            const { rows, total } = data
            this.articleList = rows
            // console.log(total)
            this.pagniationParams.totalNum = total
          } else {
            this.$message.error(message)
          }
          // 加载完毕设置为false
          this.loading = false
        })
    },
    // 页面中最大页码数改变时触发
    handleSizeChange(e) {
      this.start = 1
      this.limit = e
      this.getArticleList()
    },
    // 页面发生改变触发
    handleCurrentChange(e) {
      this.start = e
      this.getArticleList()
    }
  }
}
</script>
<style lang="scss" scoped>
.article-magin {
  .margin-30 {
    margin: 30px;
  }
}

</style>
